package cn.cloud.search.server.serarch_server.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.FSDirectory;

import cn.cloud.search.bean.query.QueryRequestInfo;
import cn.cloud.search.bean.response.SearchHitItem;
import cn.cloud.search.server.query_creater.IQueryCreator;
import cn.cloud.search.server.serarch_server.ISearchServer;
import cn.cloud.search.server.trans.ITranslation;
import cn.cloud.search.server.trans.impl.DefaultTranslation;

public class SearchServer implements ISearchServer {

	private static final Logger log = Logger.getLogger(SearchServer.class);

	private String index_dir;

	private IQueryCreator qc;

	private int pageCount = 10000;

	private Map<String, ITranslation> trans_map = new HashMap<String, ITranslation>();

	private ITranslation defaultTrans = new DefaultTranslation();

	public Map<String, ITranslation> getTrans_map() {
		return trans_map;
	}

	public void setTrans_map(Map<String, ITranslation> trans_map) {
		this.trans_map = trans_map;
	}

	public ITranslation getDefaultTrans() {
		return defaultTrans;
	}

	public void setDefaultTrans(ITranslation defaultTrans) {
		this.defaultTrans = defaultTrans;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public IQueryCreator getQc() {
		return qc;
	}

	public void setQc(IQueryCreator qc) {
		this.qc = qc;
	}

	public String getIndex_dir() {
		return index_dir;
	}

	public void setIndex_dir(String index_dir) {
		this.index_dir = index_dir;
	}

	public List<SearchHitItem> search(QueryRequestInfo qri) {
		// TODO Auto-generated method stub
		List<SearchHitItem> item_list = new ArrayList<SearchHitItem>();
		ITranslation trans = null == trans_map.get(qri.getQueryType()) ? defaultTrans
				: trans_map.get(qri.getQueryType());
		try {
			IndexReader reader = IndexReader.open(FSDirectory.open(new File(
					index_dir)), true);
			Searcher searcher = new IndexSearcher(reader);
			Query q = qc.createrQuery(qri);
			log.debug("query is"+q.toString());
			TopScoreDocCollector collector = TopScoreDocCollector.create(
					pageCount, false);
			searcher.search(q, collector);
			ScoreDoc[] hits = collector.topDocs().scoreDocs;
			for (ScoreDoc hit : hits) {
				Document doc = searcher.doc(hit.doc);
				SearchHitItem shi = trans.translation(doc);
				if(null!=shi)
					item_list.add(shi);
			}
		} catch (Exception e) {
			log.info("search server is err:" + e, e);
		}
		return item_list;
	}

}
